﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Collections;

namespace JQueryEasyUI
{
    public class DataGrid
    {
        /// <summary>
        /// 生成DataGrid data所需要的json格式。
        /// </summary>
        /// <param name="json"></param>
        /// <param name="rowsCount"></param>
        /// <returns></returns>
        public static string ToEasyUIDataGridJson(string rowsJson, int count)
        {
            return string.Format("{{\"total\":{0},\"rows\":{1}}}", count, rowsJson);
        }


        public static string ToEasyUIDataGridJson(DataTable dt, long rowsCount, params string[] columnNames)
        {
            string json = JQueryEasyUI.Tools.ToJson(dt, columnNames);
            return JQueryEasyUI.DataGrid.ToEasyUIDataGridJson(json, (int)rowsCount);
        }

        

        /// <summary>
        /// 当usingStyle为true时，fieldclass才会被用到。
        /// </summary>
        /// <param name="columns"></param>
        /// <param name="usingStyle"></param>
        /// <param name="fieldclass"></param>
        /// <returns></returns>
        public static string ToHtmlColumns(Hashtable columns, bool usingStyle, string fieldclass)
        {
            /*
             columns:[[  
        {field:'code',title:'Code',width:100},  
        {field:'name',title:'Name',width:100},  
        {field:'price',title:'Price',width:100,align:'right'}  
    ]]  

            frozenColumns
             */
            ArrayList frezonColumns = new ArrayList();
            ArrayList normalColumns = new ArrayList();
          
            if( columns.Count>0)
            {
                bool isFrozenColumn = false ;
                Hashtable propotyHash;
              foreach (object key in columns.Keys )
              {

                  if (usingStyle)
                  {
                      propotyHash = EasyUIXmlReader.GetDataGridColumnStyle(key.ToString(), columns[key].ToString(), fieldclass, out  isFrozenColumn);
                      
                  }
                  else{
                  propotyHash = new Hashtable();
                  }
                 
                  propotyHash.Add("field", string.Format("'{0}'",key));
                  propotyHash.Add("title",string.Format("'{0}'",columns[key]));
                  
                if (isFrozenColumn)
                {
                    frezonColumns.Add(propotyHash);
                }
                else 
                {
                    normalColumns.Add(propotyHash);
                }
              }

              Hashtable styleList = new Hashtable();
            }
           
               StringBuilder s1=new StringBuilder();
               string ss1 = "";
                foreach(Hashtable table in frezonColumns)
                {
                    s1.Append(ToHtmlField(table)+",");
                }
                if (s1.Length > 0)
                {
                    ss1 = s1.ToString();
                    ss1 =  CommonTools.RemoveLastString(ss1, ",");
                    ss1 = string.Format("frozenColumns:[[{0}]],", ss1);
                }
                StringBuilder s2 = new StringBuilder();
                string ss2 = "";
                foreach (Hashtable table in normalColumns)
                {
                    s2.Append(ToHtmlField(table) + ",");
                }
                if (s2.Length > 0)
                {
                    ss2 = s2.ToString();
                    ss2 =  CommonTools.RemoveLastString(ss2, ",");
                    ss2 = string.Format("columns:[[{0}]],", ss2);
                }
                string ss3 = ss1 + ss2;
                if (ss3.Length > 0) ss3 =  CommonTools.RemoveLastString(ss3, ",");
                return ss3;
        }

        public static string ToHtmlField(Hashtable table)
        {
            StringBuilder s=new StringBuilder();
            string ss = "";
            foreach (object key in table.Keys)
            { 
                s.Append(string.Format("{0}:{1},",key,table[key]));
            }
            if (s.Length > 0)
            {
                ss = s.ToString();
                ss =  CommonTools.RemoveLastString(ss, ",");
                ss = string.Format("{{{0}}}", ss);
            }
            return ss;
        }

       
        /*
        /// <summary>
        /// 计算出最合适的Width的长度。
        /// </summary>
        /// <returns></returns>
        private static int CalculateFieldWidth(string title,string data)
        { 
            
        }
         */
    }
}
